#include "head.h"

// 子线程的入口
void * pthread_fun(void *p){
    pool_t *pool = (pool_t *)p;
    
    pthread_mutex_lock(&pool->pthread_lock);
    // 去队列中拿任务
    if (pool->queue.size <= 0){
        pthread_cond_wait(&pool->cond, &pool->pthread_lock);
    }
    int net_fd = pool->queue.head->net_fd;
    // 删除
    de_queue(&pool->queue);
    pthread_mutex_unlock(&pool->pthread_lock);

    // 读取用户名
    char uname[20] = {0};
    //客户端连续三次输入错误密码的标记
    int flag_break = 0;
    //判断客户端能否登录
    login_check(net_fd, &flag_break, uname);
    //标记为1，说明客户端断开连接，关闭客户端的连接，继续下次监听
    if(flag_break == 1){
        printf("关闭客户端连接\n");
        close(net_fd);
        return NULL;
    }
    printf("---------\n");
    PathStack *stack = (PathStack *)malloc(sizeof(PathStack));
    if (stack == NULL) {
        // 内存分配失败，需要处理错误
        fprintf(stderr, "Failed to allocate memory for stack\n");
        // 可能需要退出函数或者进行其他错误处理
        return NULL; // 或者根据具体情况返回其他值
    }
    initPathStack(stack, uname);
    printf("---------\n");

    while(1){
        // 接收命令 
        char buf_cmd[100] = {0};
        int res_recv = recv(net_fd, buf_cmd, sizeof(buf_cmd), 0);   
        ERROR_CHECK(res_recv, -1, "recv");

        if (strcmp(buf_cmd, "") != 0){
            
            printf("client cmd: %s\n", buf_cmd);
            // 将命令头转换为枚举类型
            char *cmd_head;
            char *cmd_body;
            COMMAND cmd;
            if (strcmp(buf_cmd, "ls\n") == 0) {
                cmd = ls;
            } else if (strcmp(buf_cmd, "ll\n") == 0) {
                cmd = ll;
            } else if (strcmp(buf_cmd, "pwd\n") == 0) {
                cmd = pwd;
            } else {
                // 分析命令: 切割命令，获取命令头，命令体
                cmd_head = strtok(buf_cmd, " ");
                printf("cmd head : %s\n", cmd_head);
                if (strcmp(cmd_head, "upload") == 0) {
                    cmd = upload;
                    cmd_body = strtok(NULL, "\n");
                    printf("cmd body : %s\n", cmd_body);
                } else if (strcmp(cmd_head, "download") == 0) {
                    cmd = download;
                    cmd_body = strtok(NULL, "\n");
                    printf("cmd body : %s\n", cmd_body);
                } else if (strcmp(cmd_head, "cd") == 0) {
                    cmd = cd;
                    cmd_body = strtok(NULL,  " ");
                    printf("cmd body : %s\n", cmd_body);
                } else if (strcmp(cmd_head, "mkdir") == 0) {
                    cmd = mk_dir;
                    cmd_body = strtok(NULL, "\n");
                    printf("cmd body : %s\n", cmd_body);
                } else if (strcmp(cmd_head, "remove") == 0) {
                    cmd = rm_dir;
                    cmd_body = strtok(NULL,  " ");
                    printf("cmd body : %s\n", cmd_body);
                } // 无需判错，客户端已做相应处理
            }

            // 根据命令调用相应函数
            switch (cmd){
                //case upload: client_upload(stack, net_fd, cmd_body); break;
                //case download: client_download(stack, net_fd, cmd_body); break;
                case upload: recv_bigfile(stack, net_fd, cmd_body); break;
                case download: send_bigfile(stack, net_fd, cmd_body); break;
                case ls: client_check_order(stack, net_fd, "ls"); break;
                case ll: client_check_order(stack, net_fd, "ll"); break;
                case pwd: client_pwd(stack, net_fd); break;
                case mk_dir: client_mkdir(stack, net_fd, cmd_body); break;
                case rm_dir: client_remove(stack, net_fd, cmd_body); break;
                case cd: path_cd(stack, net_fd, cmd_body); break;
            }
            
            printf("operate finished\n");
            //close(net_fd);
        }else {
            continue;
        }
    }
    return NULL;
}
